2020-2021 Sunseeker Telemetry and Lighting System
eusci_b_i2c_ex5_masterMultipleSlave.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // Demo - EUSCI_B0 I2C Master TX bytes to Multiple Slaves
34 //
35 // Description: This demo connects two MSP430's via the I2C bus.
36 // The master transmits to 4 different I2C slave addresses 0x0A,0x0B,0x0C&0x0D.
37 // Each slave address has a specific related data in the array TXData[].
38 // At the end of four I2C transactions the slave address rolls over and begins
39 // again at 0x0A.
40 // ACLK = n/a, MCLK = SMCLK = BRCLK = default DCO = ~1MHz
41 //
51 //
52 //******************************************************************************
53 #include "driverlib.h"
54 
55 //*****************************************************************************
56 //
57 //Target frequency for SMCLK in kHz
58 //
59 //*****************************************************************************
60 #define CS_SMCLK_DESIRED_FREQUENCY_IN_KHZ 1000
61 
62 //*****************************************************************************
63 //
64 //SMCLK/FLLRef Ratio
65 //
66 //*****************************************************************************
67 #define CS_SMCLK_FLLREF_RATIO 30
68 
69 uint8_t TXData[]= {0xA1,0xB1,0xC1,0xD1};// Pointer to TX data
70 uint8_t SlaveAddress[]= {0x0A,0x0B,0x0C,0x0D};
71 uint8_t TXByteCtr;
72 uint8_t SlaveFlag = 0;
73 
74 void main(void)
75 {
76  WDT_A_hold(WDT_A_BASE);
77 
78  //Set Ratio and Desired MCLK Frequency and initialize DCO
79  CS_initFLLSettle(
82  );
83 
84  //Set SMCLK = DCO with frequency divider of 1
85  CS_initClockSignal(
86  CS_SMCLK,
87  CS_DCOCLKDIV_SELECT,
88  CS_CLOCK_DIVIDER_1
89  );
90 
91  //Set MCLK = DCO with frequency divider of 1
92  CS_initClockSignal(
93  CS_MCLK,
94  CS_DCOCLKDIV_SELECT,
95  CS_CLOCK_DIVIDER_1
96  );
97 
98  // Configure Pins for I2C
99  /*
100  * Select Port 5
101  * Set Pin 2, 3 to input with function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL).
102  */
103  GPIO_setAsPeripheralModuleFunctionInputPin(
104  GPIO_PORT_P5,
105  GPIO_PIN2 + GPIO_PIN3,
106  GPIO_PRIMARY_MODULE_FUNCTION
107  );
108 
109  /*
110  * Disable the GPIO power-on default high-impedance mode to activate
111  * previously configured port settings
112  */
113  PMM_unlockLPM5();
114 
115  EUSCI_B_I2C_initMasterParam param = {0};
116  param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK;
117  param.i2cClk = CS_getSMCLK();
118  param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_400KBPS;
119  param.byteCounterThreshold = 0;
120  param.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP;
121  EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, &param);
122 
123  //Set Master in receive mode
124  EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
125  EUSCI_B_I2C_TRANSMIT_MODE
126  );
127  //Enable I2C Module to start operations
128  EUSCI_B_I2C_enable(EUSCI_B0_BASE);
129 
130  EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE,
131  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
132  EUSCI_B_I2C_NAK_INTERRUPT
133  );
134 
135  //Enable master Receive interrupt
136  EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
137  EUSCI_B_I2C_TRANSMIT_INTERRUPT0 +
138  EUSCI_B_I2C_NAK_INTERRUPT
139  );
140 
141  SlaveFlag = 0;
142  while(1)
143  {
144  //Specify slave address
145  EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
147  );
148 
149 
150  TXByteCtr = 1; // Load TX byte counter
151 
152  while (EUSCI_B_I2C_SENDING_STOP == EUSCI_B_I2C_masterIsStopSent
153  (EUSCI_B0_BASE));
154 
155 
156  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
157 
158  __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts
159  // Remain in LPM0 until all data is TX'd
160 
161  // Change Slave address
162  SlaveFlag++;
163  if(SlaveFlag > 3) // Roll over slave address
164  {
165  SlaveFlag = 0;
166  __delay_cycles(1000); // Delay between transmissions
167  }
168 
169  }
170 
171 }
172 
173 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
174 #pragma vector=USCI_B0_VECTOR
175 __interrupt
176 #elif defined(__GNUC__)
177 __attribute__((interrupt(USCI_B0_VECTOR)))
178 #endif
179 void USCIB0_ISR(void)
180 {
181  switch(__even_in_range(UCB0IV, USCI_I2C_UCBIT9IFG))
182  {
183  case USCI_NONE: // No interrupts break;
184  break;
185  case USCI_I2C_UCALIFG: // Arbitration lost
186  break;
187  case USCI_I2C_UCNACKIFG: // NAK received (master only)
188  // Resend START if NAK'd
189  EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE);
190  break;
191  case USCI_I2C_UCSTTIFG: // START condition detected with own address (slave mode only)
192  break;
193  case USCI_I2C_UCSTPIFG: // STOP condition detected (master & slave mode)
194  break;
195  case USCI_I2C_UCRXIFG3: // RXIFG3
196  break;
197  case USCI_I2C_UCTXIFG3: // TXIFG3
198  break;
199  case USCI_I2C_UCRXIFG2: // RXIFG2
200  break;
201  case USCI_I2C_UCTXIFG2: // TXIFG2
202  break;
203  case USCI_I2C_UCRXIFG1: // RXIFG1
204  break;
205  case USCI_I2C_UCTXIFG1: // TXIFG1
206  break;
207  case USCI_I2C_UCRXIFG0: // RXIFG0
208  break;
209  case USCI_I2C_UCTXIFG0: // TXIFG0
210  // Check TX byte counter
211  if (TXByteCtr)
212  {
213  EUSCI_B_I2C_masterSendMultiByteNext(EUSCI_B0_BASE,
214  TXData[SlaveFlag]);
215  // Decrement TX byte counter
216  TXByteCtr--;
217  }
218  else
219  {
220  EUSCI_B_I2C_masterSendMultiByteStop(EUSCI_B0_BASE);
221  // Exit LPM0
223  }
224  break;
225  case USCI_I2C_UCBCNTIFG: // Byte count limit reached (UCBxTBCNT)
226  break;
227  case USCI_I2C_UCCLTOIFG: // Clock low timeout - clock held low too long
228  break;
229  case USCI_I2C_UCBIT9IFG: // Generated on 9th bit of a transmit (for debugging)
230  break;
231  default:
232  break;
233  }
234 }
235 
#define CS_SMCLK_FLLREF_RATIO
#define CS_SMCLK_DESIRED_FREQUENCY_IN_KHZ
MPU_initThreeSegmentsParam param
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)